<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="前缀和与差分前缀和可快速访问一段区间的和 前缀和的构建12345solve&#123;	vector&lt;int&gt; a(n+1);	vector&lt;int&gt; sum(n+1);    for(int i&#x3D;1;i&lt;&#x3D;n;i++) sum[i]&#x3D;sum[i-1]+a[i];&#125;  访问p,q区间12int p,q,ans;ans&#x3D;sum[q]-sum[p-1];  二维">
<meta property="og:type" content="article">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="http://example.com/2025/02/19/%E5%89%8D%E7%BC%80%E5%92%8C%E4%B8%8E%E5%B7%AE%E5%88%86/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="前缀和与差分前缀和可快速访问一段区间的和 前缀和的构建12345solve&#123;	vector&lt;int&gt; a(n+1);	vector&lt;int&gt; sum(n+1);    for(int i&#x3D;1;i&lt;&#x3D;n;i++) sum[i]&#x3D;sum[i-1]+a[i];&#125;  访问p,q区间12int p,q,ans;ans&#x3D;sum[q]-sum[p-1];  二维">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2025-02-19T12:29:12.678Z">
<meta property="article:modified_time" content="2025-02-16T15:19:15.575Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/fork-awesome@1.2.0/css/fork-awesome.min.css">

<meta name="generator" content="Hexo 7.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-前缀和与差分" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2025/02/19/%E5%89%8D%E7%BC%80%E5%92%8C%E4%B8%8E%E5%B7%AE%E5%88%86/" class="article-date">
  <time class="dt-published" datetime="2025-02-19T12:29:12.678Z" itemprop="datePublished">2025-02-19</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="前缀和与差分"><a href="#前缀和与差分" class="headerlink" title="前缀和与差分"></a>前缀和与差分</h1><h2 id="前缀和"><a href="#前缀和" class="headerlink" title="前缀和"></a>前缀和</h2><p>可快速访问一段区间的和</p>
<h3 id="前缀和的构建"><a href="#前缀和的构建" class="headerlink" title="前缀和的构建"></a>前缀和的构建</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">solve&#123;</span><br><span class="line">	<span class="function">vector&lt;<span class="type">int</span>&gt; <span class="title">a</span><span class="params">(n<span class="number">+1</span>)</span></span>;</span><br><span class="line">	<span class="function">vector&lt;<span class="type">int</span>&gt; <span class="title">sum</span><span class="params">(n<span class="number">+1</span>)</span></span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++) sum[i]=sum[i<span class="number">-1</span>]+a[i];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="访问p-q区间"><a href="#访问p-q区间" class="headerlink" title="访问p,q区间"></a>访问p,q区间</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> p,q,ans;</span><br><span class="line">ans=sum[q]-sum[p<span class="number">-1</span>];</span><br></pre></td></tr></table></figure>

<h3 id="二维前缀和"><a href="#二维前缀和" class="headerlink" title="二维前缀和"></a>二维前缀和</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">solve&#123;</span><br><span class="line">	vector&lt;vector&lt;<span class="type">int</span>&gt;&gt; <span class="built_in">a</span>(n<span class="number">+1</span>,<span class="built_in">vector</span>&lt;<span class="type">int</span>&gt;(n<span class="number">+1</span>));</span><br><span class="line">	vector&lt;vector&lt;<span class="type">int</span>&gt;&gt; <span class="built_in">sum</span>(n<span class="number">+1</span>,<span class="built_in">vector</span>&lt;<span class="type">int</span>&gt;(n<span class="number">+1</span>));</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=n;i++)</span><br><span class="line">            sum[i][j]=sum[i<span class="number">-1</span>][j]+sum[i][j<span class="number">-1</span>]-sum[i<span class="number">-1</span>][j<span class="number">-1</span>]+a[i][j];</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="访问-p-q-到-m-n-区间"><a href="#访问-p-q-到-m-n-区间" class="headerlink" title="访问(p,q)到(m,n)区间"></a>访问(p,q)到(m,n)区间</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> p,q,m,n,ans;</span><br><span class="line">ans=sum[m][n]-sum[p<span class="number">-1</span>][n]-sum[m][q<span class="number">-1</span>]+sum[p<span class="number">-1</span>][q<span class="number">-1</span>];</span><br></pre></td></tr></table></figure>



<h2 id="差分"><a href="#差分" class="headerlink" title="差分"></a>差分</h2><p>可快速解决对多次修改区间的值</p>
<h3 id="差分的构建"><a href="#差分的构建" class="headerlink" title="差分的构建"></a>差分的构建</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">vector&lt;<span class="type">int</span>&gt; <span class="title">a</span><span class="params">(n<span class="number">+1</span>)</span></span>;</span><br><span class="line"><span class="function">vector&lt;<span class="type">int</span>&gt; <span class="title">differ</span><span class="params">(n<span class="number">+1</span>)</span></span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++) differ[i]=a[i]-a[i<span class="number">-1</span>];</span><br></pre></td></tr></table></figure>

<h3 id="对p-q区间修改x"><a href="#对p-q区间修改x" class="headerlink" title="对p,q区间修改x"></a>对p,q区间修改x</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">differ[p]+=x;</span><br><span class="line">differ[q<span class="number">+1</span>]-=x;</span><br></pre></td></tr></table></figure>

<h3 id="还原成原数组"><a href="#还原成原数组" class="headerlink" title="还原成原数组"></a>还原成原数组</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">a[i]=differ[i]+a[i<span class="number">-1</span>];</span><br></pre></td></tr></table></figure>



<h3 id="二维差分构建"><a href="#二维差分构建" class="headerlink" title="二维差分构建"></a>二维差分构建</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">vector&lt;vector&lt;<span class="type">int</span>&gt;&gt; <span class="built_in">a</span>(n<span class="number">+1</span>,<span class="built_in">vector</span>&lt;<span class="type">int</span>&gt;(n<span class="number">+1</span>));</span><br><span class="line">vector&lt;vector&lt;<span class="type">int</span>&gt;&gt; <span class="built_in">differ</span>(n<span class="number">+1</span>,<span class="built_in">vector</span>&lt;<span class="type">int</span>&gt;(n<span class="number">+1</span>));</span><br><span class="line"><span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">           differ[i][j]+=a[i][j];</span><br><span class="line">       	differ[i<span class="number">+1</span>][j]-=a[i][j];</span><br><span class="line">       	differ[i][j<span class="number">+1</span>]-=a[i][j];</span><br><span class="line">       	differ[i<span class="number">+1</span>][j<span class="number">+1</span>]+=a[i][j];&#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<h3 id="对-p-q-到-m-n-区间修改x"><a href="#对-p-q-到-m-n-区间修改x" class="headerlink" title="对(p,q)到(m,n)区间修改x"></a>对(p,q)到(m,n)区间修改x</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">differ[p][q]+=x;</span><br><span class="line">   differ[p][n<span class="number">+1</span>]-=x;</span><br><span class="line">   differ[m<span class="number">+1</span>][q]-=x;</span><br><span class="line">   differ[m<span class="number">+1</span>][n<span class="number">+1</span>]+=x;</span><br></pre></td></tr></table></figure>

<h3 id="还原成原数组-1"><a href="#还原成原数组-1" class="headerlink" title="还原成原数组"></a>还原成原数组</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=n;j++)</span><br><span class="line">          a[i][j]=differ[i][j]+a[i][j<span class="number">-1</span>]+a[i<span class="number">-1</span>][j]-a[i<span class="number">-1</span>][j<span class="number">-1</span>];</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>


      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2025/02/19/%E5%89%8D%E7%BC%80%E5%92%8C%E4%B8%8E%E5%B7%AE%E5%88%86/" data-id="cm7bw4ln50001aoa92d4rhzis" data-title="" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2025/02/19/%E8%BE%97%E8%BD%AC%E7%9B%B8%E9%99%A4%E6%B3%95/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="/2025/02/19/%E4%BA%8C%E5%88%86%E6%B3%95/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2025/02/">February 2025</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2025/02/19/%E8%BE%97%E8%BD%AC%E7%9B%B8%E9%99%A4%E6%B3%95/">(no title)</a>
          </li>
        
          <li>
            <a href="/2025/02/19/%E5%89%8D%E7%BC%80%E5%92%8C%E4%B8%8E%E5%B7%AE%E5%88%86/">(no title)</a>
          </li>
        
          <li>
            <a href="/2025/02/19/%E4%BA%8C%E5%88%86%E6%B3%95/">(no title)</a>
          </li>
        
          <li>
            <a href="/2025/02/19/Untitled/">(no title)</a>
          </li>
        
          <li>
            <a href="/2025/02/19/%E7%AC%AC%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0/">第一篇文章</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2025 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>